#include <gtest/gtest.h>
#include <mockcpp/mockcpp.hpp>
#include "co_routine.h"
#include <stdio.h>
#include <stdlib.h>
#include "test_main.h"

static void* co_entry(void* arg) {
    int* counter = (int*)arg;
    while(1) {
        printf("Coroutine: counter = %d\n", ++(*counter));
        co_yield_ct();
    }
    return NULL;
}

TEST(LibcoTest, BasicFunctionality) {
    int counter = 0;
    stCoRoutine_t* co = NULL;
    co_create(&co, NULL, co_entry, &counter);
    
    for(int i = 0; i < 5; i++) {
        co_resume(co);
        printf("Main: counter = %d\n", counter);
    }
    
    EXPECT_GE(counter, 5);
}
#include <stdlib.h>

extern "C" {
#include "thread_intf.h"
#include "log_intf.h"
#include "lvos.h"
    
extern int32_t InitAllModules(void);
extern void DeInitAllModules(void);
extern void WebcliEventLoopTask(void* arg);
}

int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  int result = RUN_ALL_TESTS();
  exit(result); // 确保覆盖率数据写入
}

class ModuleTest : public testing::Test {
    protected:
    void SetUp() override {
        MOCKER(WebcliEventLoopTask).stubs().will(returnValue(NULL));
        InitAllModules();
    }
    void TearDown() override {
        DeInitAllModules();
    }
};

void SayHello(void *ctx)
{
    LOG_INFO("Test Hello");
}

TEST_F(ModuleTest, thread_pool_multi_instance)
{
    TPool* pool1 = nullptr;
    TPool* pool2 = nullptr;
    
    // 创建两个线程池实例
    EXPECT_EQ(CreatePThreadPool(&pool1, 2), RETURN_OK);
    EXPECT_EQ(CreatePThreadPool(&pool2, 3), RETURN_OK);
    EXPECT_NE(pool1, pool2);
    
    // 测试向不同线程池添加任务
    static int counter1 = 0;
    static int counter2 = 0;
    
    auto task1 = [](void* arg) { (*(int*)arg)++; };
    auto task2 = [](void* arg) { (*(int*)arg) += 2; };
    
    for(int i = 0; i < 5; i++) {
        EXPECT_EQ(ThreadPoolAddTask(pool1, task1, &counter1), RETURN_OK);
        EXPECT_EQ(ThreadPoolAddTask(pool2, task2, &counter2), RETURN_OK);
    }
    
    // 等待任务完成
    sleep(1);
    EXPECT_GE(counter1, 5);
    EXPECT_GE(counter2, 10);
    
    // 销毁线程池
    ThreadPoolDestroy(pool1);
    ThreadPoolDestroy(pool2);
}

TEST_F(ModuleTest, create_lwt_success)
{
    int32_t ret = LWT_CREATE(SayHello, NULL);
    ret = LWT_CREATE(SayHello, NULL);
    EXPECT_EQ(ret, RETURN_OK);
}
